package com.itheima.manager;



import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.PageResult;
import com.itheima.common.ResultData;
import com.itheima.domain.*;
import com.itheima.service.*;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UmsRoleManager {

    @DubboReference
    private UmsRoleService umsRoleService;

    @DubboReference
    private UmsRoleMenuRelationService umsRoleMenuRelationService;

    @DubboReference
    private UmsMenuService umsMenuService;

    @DubboReference
    private UmsRoleResourceRelationService umsRoleResourceRelationService;

    @DubboReference
    private UmsResourceService umsResourceService;
    // 查询全部
    public ResultData getAll() {
        List<UmsRole> all = umsRoleService.getAll();
        return ResultData.ok(all,"");
    }

    // 分页查询
    public ResultData getListByPage(Integer pageNum, Integer pageSize, String keyword) {
        Page<UmsRole> listByPage = umsRoleService.getListByPage(pageNum, pageSize, keyword);
        return ResultData.ok(new PageResult(pageNum,pageSize,listByPage.getTotal(),listByPage.getPages(),listByPage.getRecords()),"");
    }

    // 添加角色
    public ResultData create(UmsRole umsRole) {
        // 角色名称为空返回错误信息提示
        if(StringUtils.isEmpty(umsRole.getName())){
            return ResultData.error( "角色名称不能为空!");
        }
        //umsRole.setCreateTime(DateUtil.toLocalDateTime(new Date()));
        Integer count = umsRoleService.create(umsRole);
        return ResultData.ok(count, "操作成功!");
    }

    // 批量删除角色
    public ResultData delete(List<Long> ids) {
        Integer count = umsRoleService.deleteByIds(ids);
        return ResultData.ok(count,"删除成功");
    }

    // 修改角色
    public ResultData update(UmsRole umsRole) {
        // 角色名称为空返回错误信息提示
        if(StringUtils.isEmpty(umsRole.getName())){
            return ResultData.error( "角色名称不能为空!");
        }
        Integer count = umsRoleService.update(umsRole);
        return ResultData.ok(count,"修改成功");
    }

    // 修改角色状态
    public ResultData updateStatus(Integer id, Integer status) {
        umsRoleService.updateStatus(id,status);
        return ResultData.ok(id,"修改成功");
    }

    // 获取角色相关菜单
    public ResultData listMenu(Long roleId) {
        List<UmsRoleMenuRelation> umsRoleMenuRelations = umsRoleMenuRelationService.getByRoleId(roleId);
        /*if(CollUtil.isEmpty(umsRoleMenuRelations)){
            return new ArrayList<>();
        }*/
        // 提取 menuId
        List<Long> menuIds = CollUtil.getFieldValues(umsRoleMenuRelations, "menuId", Long.class);
        List<UmsMenu> menus = umsMenuService.getByIds(menuIds);
        return ResultData.ok(menus,"");

    }

    // 给角色分配菜单
    public ResultData allocMenu(Long roleId, List<Long> menuIds) {
        // 1. 首先根据 roleId 删除 对应的数据
        umsRoleMenuRelationService.removeByRoleId(roleId);
        // 2. 不为空,执行添加操作
        if(CollUtil.isNotEmpty(menuIds)){
            // 2.1 添加传来的menuIds
            // 遍历添加
            for (Long menuId : menuIds) {
                UmsRoleMenuRelation umsRoleMenuRelation = new UmsRoleMenuRelation();
                umsRoleMenuRelation.setMenuId(menuId);
                umsRoleMenuRelation.setRoleId(roleId);
                umsRoleMenuRelationService.save(umsRoleMenuRelation);
            }
        }
        return ResultData.ok("","");
    }

    // 获取角色相关资源
    public ResultData listResource(Long roleId) {
        // 根据 roleId 查询对应的 resourceId
        List<UmsRoleResourceRelation> umsRoleResourceRelations = umsRoleResourceRelationService.getByRoleId(roleId);
       /* if(CollUtil.isEmpty(umsRoleResourceRelations)){
            return new ArrayList<>();
        }*/
        List<Long> resourceIds = CollUtil.getFieldValues(umsRoleResourceRelations, "resourceId", Long.class);
        // 根据 id 集合批量查询
        List<UmsResource> umsResources = umsResourceService.getByIds(resourceIds);
        return ResultData.ok(umsResources,"");
    }


    // 给角色分配资源
    public ResultData allocResource(Long roleId, List<Long> resourceIds) {
        // 1. 首先根据 roleId 删除 对应的数据
        umsRoleResourceRelationService.removeByRoleId(roleId);
        // 2. 不为空,执行添加操作
        if(CollUtil.isNotEmpty(resourceIds)){
            // 2.1 添加传来的resourceIds
            // 遍历添加
            for (Long resourceId : resourceIds) {
                UmsRoleResourceRelation umsRoleMenuRelation = new UmsRoleResourceRelation();
                umsRoleMenuRelation.setResourceId(resourceId);
                umsRoleMenuRelation.setRoleId(roleId);
                umsRoleResourceRelationService.save(umsRoleMenuRelation);
            }
        }
        return ResultData.ok("","");

    }
}
